// ==UserScript==
// @name         table
// @namespace    http://tampermonkey.net/
// @version      1.991
// @description  try to take over the world!
// @author       You
//@match         http*://*/*
// @require      https://cdn.bootcss.com/jquery/1.12.3/jquery.min.js
// @require      https://chenxqiyu.gitee.io/source/layer/layer.js
//@require https://gitee.com/chenxqiyu/Node/raw/master/tam/table.js
// @grant        none
// ==/UserScript==
var lay;
var table;
var isShow = true;
(function () {
    'use strict';


    window.onload = function () {

        if (document.location.href.indexOf("/g/lol") != -1) {
            huya();
        }
        // var u = '<div style="text-align:center">';
        // $("img").each(function () {
        //     try {
        //         var host = "";
        //         if ($(this).attr('src').indexOf("//") == -1)
        //             host = window.origin + "/"; {
        //         }
        //         u += $("<img>").attr("src", host + $(this).attr('src')).attr("style", "width:100%").prop("outerHTML") + "<br>";
        //     } catch (e) { }

        // })


    }


    function huya() {
        var u = '<div style="text-align:center">';
        $("#login-userAvatar").remove()

        var select1;

        try {
            select1 = ".game-live-item";
            $(select1).each(function () {

                var title = $(this).find("a.title.new-clickstat").text();
                var href = $(this).find("a.video-info.new-clickstat").attr("href");
                var src = $(this).find("a.video-info.new-clickstat > img").attr("data-original")

                var re = src.split('?')[1];
                src = src.replace(re, "x-oss-process=image/resize,fixed,w_3000,limit_0/crop,x_0,y_0,w_580,h_50,g_ne");


                u += '<a target="_blank" href=' + href + '><img src=' + src + '/><br>' + title + '</a><br>'
            })

        } catch (e) { }

        u += "</div>";
        layer.open({
            type: 1,
            id: 'w',
            shade: 0,
            title: '嵌入悬浮窗',
            maxmin: true,
            area: ['70%', '90%'],
            content: u,
            end: function () {


            }
        });
    }
    // $("#w").html(u)




    function pic(d) {
        var id = 0;
        for (const key in document.images) {
            try {
                const element = document.images[key];
                if (element.width > '100') {
                    // console.log(element);
                    d.put("CN" + id, {
                        图片编号: id,
                        图片地址: element.src.replace("330x190", "690x0"),
                        图片链接: element.parentElement.href,

                    });
                }
            } catch (error) {
                continue;
            }
            //if (document.images.hasOwnProperty(key)) {

            //}
            id++;
        }
        // dic.push()
    }
    //setInterval(function () {
    try {
        var d = new Dictionary();
        // 速报1(d);
        // 速报2(d);
        //pic(d);

        //pushWindow(d);
        // createTable(450, 270, d, Object.keys(d.get(d.keys[0])).length);

    } catch (e) { }
    // };
    // }, 1000);

    function pushData(data) {
        $.ajax({
            type: "post", //要用post方式
            url: "http://localhost:8989/Service/WebDataService.asmx/writeData?t=0",//方法所在页面和方法名
            data: {
                //webCode: $("#list").val().toString(),
                msg: data
                //msg: HTMLEncode(document.body.innerHTML)
                // clipboardData.getData('text')
            },
            success: function (data) {

                //window.close();
                // getCopyData();//是否自动播放

            },
            error: function (err) {
                if (err.readyState == 4 && err.statusText == "OK") {
                    //alert(err);
                } else {
                    //alert(err.responseText);
                }
            }
        });
    }
    function pushWindow(d) {
        var json = "[";
        for (let i = 0; i < d.keys.length; i++) {
            json += JSON.stringify(d.get(d.keys[i]));
            if (i != d.keys.length - 1) {
                json += ",";
            }
        }
        json += "]";
        // console.log(json);
        pushData(json);
    }
    function Dictionary() {
        this.data = {};

        this.keys = new Array();
        this.put = function (key, value) {
            this.data[key] = value;
            this.keys.push(key);

        };

        this.get = function (key) {
            return this.data[key];
        };

        this.remove = function (key) {
            this.data[key] = null;
        };

        this.isEmpty = function () {
            return this.data.length == 0;
        };

        this.size = function () {
            return this.data.length;
        };
    }

    //td{ text-align:center;} 2、<td align="center" valign="middle"> 前一个是水平居中 后一个是垂直居中
    //创建表格
    function createTable(w, h, dic, c) {
        table = document.createElement("table");
        table.id = "abc";
        table.style.width = '100%';
        table.style.heigth = '100%';
        // table.width = w;
        table.height = h;
        table.border = 1;
        table.cellPadding = 3;
        table.cellSpacing = 1;
        table.bgColor = "#c1c1c1";

        // table.setAttribute("style","text-align: center;");

        var tbody = document.createElement("tbody");
        for (var i = 1; i <= dic.keys.length + 1; i++) {
            var tr = document.createElement("tr");
            tr.bgColor = "#FFFFFF";
            for (var j = 1; j <= c; j++) {
                try {
                    var td = document.createElement("td");
                    if (i == 1) {
                        td.innerHTML = Object.keys(dic.get(dic.keys[0]))[j - 1];
                    }
                    else if (j == c) {
                        //var img = document.createElement("img");
                        var name = Object.keys(dic.get(dic.keys[i - 2]))[j - 1];
                        var json = dic.get(dic.keys[i - 2]);
                        var obj = json[name];
                        if (obj != null && obj != undefined && obj != '') {
                            td.appendChild(obj.parentElement);
                        } else {
                            var button = document.createElement("button");
                            button.innerHTML = "Button" + i;
                            button.addEventListener("click", function (e) { alert("test") }, false);
                            td.appendChild(button);
                        }
                    }
                    else {
                        var name2 = Object.keys(dic.get(dic.keys[i - 2]))[j - 1];
                        var json2 = dic.get(dic.keys[i - 2]);
                        td.innerHTML = json2[name2];
                        // td.innerHTML = i + "" + j;
                    }
                    // td.style.color = "#FF0000";
                    td.setAttribute("style", "text-align: center;");
                    tr.appendChild(td);
                } catch (e) { }
            }

            tbody.appendChild(tr);
        }
        table.appendChild(tbody);
        try {
            // $("#w").html("");
            //$("#w").removeChild(document.getElementById("abc"));
        } catch (e) { }

        var show = u;

        layer.open({
            type: 1,
            id: 'w',
            shade: 0,
            title: '嵌入悬浮窗',
            maxmin: true,
            area: ['800px', '90%'],
            content: table.outerHTML,
            end: function () {


            }
        });

        // $("#w").append(table);
    }
    if (isShow) {

        $("head").append('<link rel="stylesheet" href="https://chenxqiyu.gitee.io/source/layer/theme/default/layer.css?v=3.1.1" id="layuicss-layer">');

    }

})();